package cn.tl.pet.controller;

import cn.tl.org.utils.AjaxResult;
import cn.tl.org.utils.QueryResult;
import cn.tl.pet.domain.Pet;
import cn.tl.pet.query.PetQuery;
import cn.tl.pet.service.IPetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {

    @Autowired
    private IPetService petService;
    @GetMapping("/detail/{id}")
    public Pet page(@PathVariable Long id){
        return petService.loadByIdDetail(id);
    }
    @PostMapping
    public QueryResult<Pet> page(@RequestBody PetQuery query){
        return petService.queryPage(query);
    }
    @PostMapping("/onsale")
    public AjaxResult onsale(@RequestBody List<Long> list){
        try {
            petService.onsale(list);
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }

    }
    @PostMapping("/offsale")
    public AjaxResult offsale(@RequestBody List<Long> list){
        try {
            petService.offsale(list);
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }

    }
    @PutMapping
    public AjaxResult offsale(@RequestBody Pet pet){
        try {
            petService.save(pet);
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }

    }
}
